package com.wwu.system.controller;

import com.wwu.common.web.ResultObj;
import com.wwu.system.common.utils.OshiUtils;
import com.wwu.system.entity.server.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;

/**
 * 服务器监控前端控制器
 * @author 一蓑烟雨
 * @version 1.0.0
 * @date 2023-07-10 10:31
 **/
@Api(tags = "服务器监控控制器(ServerController)")
@ApiSort(2)
@Controller
@RequestMapping("/system/server")
public class ServerController {
    @Resource
    private OshiUtils oshiUtils;

    /**
     * 请求转发到服务监控主页
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/7/11 10:46
     */
    @GetMapping("/list")
    public String serverInfoHtml() {
        return "system/server/server-info";
    }

    @ApiOperation(value = "查询服务器全部信息")
    @ApiOperationSupport(order = 1)
    @GetMapping("/allInfo")
    @ResponseBody
    public ResultObj getServerAllInfo(){
        Server server = new Server();
        //设置操作系统信息
        server.setOperSys(oshiUtils.getOperSysInfo());
        //设置CPU信息
        server.setCpu(oshiUtils.getCpuInfo());
        //设置内存信息
        server.setMem(oshiUtils.getMemInfo());
        //设置虚拟机信息
        server.setJvm(oshiUtils.getJvmInfo());
        //设置硬盘信息
        server.setDiscs(oshiUtils.getDiscs());

        return ResultObj.success("服务器信息查询成功",server);
    }

    @ApiOperation(value = "查询CPU信息")
    @ApiOperationSupport(order = 2)
    @GetMapping("/cpuInfo")
    @ResponseBody
    public ResultObj getServerCpuInfo(){
        Cpu cpu = oshiUtils.getCpuInfo();
        return ResultObj.success("服务器信息查询成功",cpu);
    }

    @ApiOperation(value = "查询内存信息")
    @ApiOperationSupport(order = 3)
    @GetMapping("/memInfo")
    @ResponseBody
    public ResultObj getServerMemInfo(){
        Mem mem = oshiUtils.getMemInfo();
        return ResultObj.success("内存信息查询成功",mem);
    }

    @ApiOperation(value = "查询硬盘信息")
    @ApiOperationSupport(order = 4)
    @GetMapping("/discInfo")
    @ResponseBody
    public ResultObj getServerDiscInfo(){
        List<Disc> discs = oshiUtils.getDiscs();
        return ResultObj.success("硬盘信息查询成功",discs);
    }

    @ApiOperation(value = "查询虚拟机信息")
    @ApiOperationSupport(order = 5)
    @GetMapping("/jvmInfo")
    @ResponseBody
    public ResultObj getServerJvmInfo(){
        Jvm jvm = oshiUtils.getJvmInfo();
        return ResultObj.success("虚拟机信息查询成功",jvm);
    }

    @ApiOperation(value = "查询操作系统信息")
    @ApiOperationSupport(order = 6)
    @GetMapping("/osInfo")
    @ResponseBody
    public ResultObj getServerOperSysInfo(){
        OperSys operSys  = oshiUtils.getOperSysInfo();
        return ResultObj.success("操作系统信息查询成功",operSys);
    }
}
